<?php

namespace app\installs;


class MySqlInstall extends BaseInstall {

    public function up() {
        $this->createTable($this->settingTable, [
            'id' => $this->string(70)->notNull(),
            'value' => $this->string(500)->notNull()->defaultValue(''),
            'user_id' => $this->integer(10)->notNull()->defaultValue(0),
            'PRIMARY KEY ([[id]], [[user_id]])',
        ], $this->tableOptions);

        $this->createTable($this->metaTable, [
            'id' => $this->primaryKey(),
            'parent_id' => $this->integer()->notNull()->defaultValue('0'),
            'type' => $this->smallInteger()->notNull()->defaultValue(0),
            'name' => $this->string(160)->notNull()->defaultValue(''),
            'description' => $this->string(200)->notNull()->defaultValue(''),
            'slug' => $this->char(100)->notNull()->defaultValue(''),
            'order' => $this->smallInteger()->notNull()->defaultValue('0'),
            'count' => $this->integer()->notNull()->defaultValue('0'),
            'is_hide' => $this->smallInteger()->notNull()->defaultValue('0'),
            'UNIQUE KEY ([[slug]])'
        ], $this->tableOptions);

        $this->createTable($this->relationshipTable, [
            'post_id' => $this->integer()->notNull()->defaultValue(0),
            'meta_id' => $this->integer()->notNull()->defaultValue(0),
            'PRIMARY KEY ([[post_id]], [[meta_id]])'
        ], $this->tableOptions);

        $this->createTable($this->postTable, [
            'id' => $this->primaryKey(),
            'parent_id' => $this->integer()->notNull()->defaultValue('0'),
            'uid' => $this->integer()->notNull()->defaultValue('0'),
            'type' => $this->smallInteger()->notNull()->defaultValue(0),
            'cid' => $this->integer()->notNull()->defaultValue('0'),
            'title' => $this->string(200)->notNull()->defaultValue(''),
            'content' => $this->text()->notNull(),
            'thumbnail' => $this->string(160)->notNull()->defaultValue(''),
            'view_count' => $this->integer()->notNull()->defaultValue('0'),
            'comment_count' => $this->integer()->notNull()->defaultValue('0'),
            'up_count' => $this->integer()->notNull()->defaultValue('0'),
            'down_count' => $this->integer()->notNull()->defaultValue('0'),
            'is_top' => $this->smallInteger()->notNull()->defaultValue(0),
            'is_comment' => $this->smallInteger()->notNull()->defaultValue(0),
            'is_publish' => $this->smallInteger()->notNull()->defaultValue(0),
            'create_time' => $this->integer()->notNull()->defaultValue('0'),
            'update_time' => $this->integer()->notNull()->defaultValue('0'),
            'KEY ([[cid]])',
        ], $this->tableOptions);

        $this->createTable($this->userTable, [
            'id' => $this->primaryKey(),
            'gid' => $this->smallInteger()->notNull()->defaultValue('0'),
            'username' => $this->string(120)->notNull()->defaultValue(''),
            'nickname' => $this->string(120)->notNull()->defaultValue(''),
            'password' => $this->char(16)->notNull()->defaultValue(''),
            'avatar' => $this->string(120)->notNull()->defaultValue(''),
            'email' => $this->string(120)->notNull()->defaultValue(''),
            'home_url' => $this->string(120)->notNull()->defaultValue(''),
            'token' => $this->string(120)->notNull()->defaultValue(''),
            'last_login_time' => $this->integer()->notNull()->defaultValue(0),
            'last_login_ip' => $this->integer()->notNull()->defaultValue(0),
            'status' => $this->smallInteger()->notNull()->defaultValue(0),
            'create_time' => $this->integer()->notNull()->defaultValue('0'),
            'update_time' => $this->integer()->notNull()->defaultValue('0'),
            'KEY ([[gid]])',
            'UNIQUE KEY ([[username]])'
        ], $this->tableOptions);

        $this->createTable($this->commentTable, [
            'id' => $this->primaryKey(),
            'owner_id' => $this->integer()->notNull()->defaultValue('0'),
            'owner_type' => $this->smallInteger()->notNull()->defaultValue('0'),
            'parent_id' => $this->integer()->notNull()->defaultValue('0'),
            'comment_author' => $this->string(120)->notNull()->defaultValue(''),
            'comment_author_email' => $this->string(120)->notNull()->defaultValue(''),
            'comment_author_url' => $this->string(120)->notNull()->defaultValue(''),
            'comment_author_ip' => $this->string(120)->notNull()->defaultValue(''),
            'comment_content' => $this->string(120)->notNull()->defaultValue(''),
            'comment_status' => $this->smallInteger()->notNull()->defaultValue(0),
            'comment_up' => $this->integer()->notNull()->defaultValue('0'),
            'comment_down' => $this->integer()->notNull()->defaultValue('0'),
            'user_id' => $this->integer()->notNull()->defaultValue(0),
            'create_time' => $this->integer()->notNull()->defaultValue('0'),
            'update_time' => $this->integer()->notNull()->defaultValue('0'),
            'KEY ([[owner_type]], [[owner_id]])',
        ], $this->tableOptions);
    }

    public function down() {
        $this->dropTable($this->settingTable);
        $this->dropTable($this->metaTable);
        $this->dropTable($this->postTable);
        $this->dropTable($this->relationshipTable);
        $this->dropTable($this->userTable);
        $this->dropTable($this->commentTable);
    }
}